package com.woniu.dao;

import com.woniu.entity.User;
import com.woniu.entity.UserAnswer;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface UserDao {
    @Select("select * from user order by create_time desc")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "isDelete", property = "isdelete"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<User> selectAllUsers();

    @Select("<script>" +
            "select * from user " +
            "<where>" +
            "<if test='username != null'> and username like concat('%', #{username}, '%')</if>" +
            "<if test='gender != null'> and gender = #{gender}</if>" +
            "<if test='status != null'> and status = #{status}</if>" +
            "</where>" +
            "order by create_time desc" +
            "</script>")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "isDelete", property = "isdelete"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<User> selectByCondition(User user);

    @Insert("insert into user (role_id, username, password, phone, status, gender, balance, points, email, avatar) " +
            "values (#{roleId}, #{username}, #{password}, #{phone}, #{status}, #{gender}, #{balance}, #{points}, #{email}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insertUser(User user);

    @Update("update user set " +
            "role_id = #{roleId}, " +
            "username = #{username}, " +
            "password = #{password}, " +
            "phone = #{phone}, " +
            "status = #{status}, " +
            "audit_status = #{auditStatus}, " +
            "gender = #{gender}, " +
            "balance = #{balance}, " +
            "points = #{points}, " +
            "email = #{email}, " +
            "avatar = #{avatar} " +
            "where user_id = #{userId}")
    int updateUser(User user);

    @Delete("delete from user where user_id = #{id}")
    int deleteUser(Integer id);

    @Select("select * from user where user_id = #{id}")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "isDelete", property = "isdelete"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    User getUserById(Integer id);

    @Update("update user set status = #{status} where user_id = #{userId}")
    int updateUserStatus(User user);

    @Select("select * from user where username= #{username}")
    User selectByUserName(String username);

    // 统计待审核教师数量
    Long countPendingTeachers();

    // 统计新增用户数量
    Long countNewUsers();

    //教师列表
    @Select("select * from user where role_id = 2 and isDelete = 0 and status = 1 order by create_time desc")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "isDelete", property = "isdelete"),
            @Result(column = "status", property = "status"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<User> selectTeachers();

    @Select("select * from user where role_id = 2")
    List<User> getTeachers();
    @Insert("insert into collect_user_course (uid,cid)" +
            "values (#{uid},#{cid})")
    void addCollect(@Param("uid") Integer uid,@Param("cid") Integer cid);
    //取消收藏
    @Delete("delete from collect_user_course where uid = #{uid} and cid = #{cid}")
    void delCollect(@Param("uid") Integer uid,@Param("cid") Integer cid);

    @Insert("insert into user_video (user_id, video_id, test) values (#{userId},#{videoId},#{test})")
    void addUserAnswer(UserAnswer userAnswer);

    @Select("select * from user_video where user_id=#{userId} and video_id=#{videoId}")
    UserAnswer selectByUserIdAndVedioId(Integer userId, Integer videoId);

    @Select("select * from user_video where user_id=#{userId}")
    List<UserAnswer> selectByUserId(Integer userId);

    @Update("update user_video set test=#{test} where id=#{id}")
    void updateUserAnswer(UserAnswer userAnswer);

    @Delete("delete from  user_video where user_id=#{userId} and video_id=#{videoId} ")
    void deleteUserAnswer(Integer userId, Integer videoId);
}
